package com.lsc.record.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.lsc.record.aop.annotation.OperationLogDesc;
import com.lsc.record.controller.parameter.PageInfo;
import com.lsc.record.core.service.DocumentServcie;
import com.lsc.record.core.util.RowConvertUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.row.Row;

@RequestMapping(value = "doc")
@RestController
public class DocumentController extends ApiBaseController<Map<String, Object>> {

	@Resource
	private DocumentServcie documentService;

	@OperationLogDesc(module = "文档服务>查询", events = "分页查询")
	@PostMapping(value = "pagelist")
	public Page<?> pageList(@RequestBody JSONObject requestbody) {
		PageInfo pageInfo = requestbody.getJSONObject("pageInfo").to(PageInfo.class);
		String typeName = requestbody.getString("typeName");
		JSONArray parameters = requestbody.getJSONArray("parameters");
		Page<Row> result = documentService.queryPage(pageInfo, typeName, parameters);
		return result;
	}

	@OperationLogDesc(module = "文档服务>新建", events = "文档数据创建")
	@Override
	public String save(@RequestBody Map<String, Object> data) throws Exception {
		Row row = new Row();
		row.putAll(data);
		data = row.toUnderlineKeysMap();
		row = new Row();
		row.putAll(data);
		String typeName = row.getString("type_name");
		row = documentService.create(typeName, data);
		return row.getString("id");
	}

	@SuppressWarnings("unchecked")
	@OperationLogDesc(module = "文档服务>新建", events = "新建文档数据和上传文件")
	@PostMapping("createdoc")
	public String save(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> doc)
			throws Exception {

		Map<String, Object> formdata = JSON.parseObject(doc.get("doc").toString(), HashMap.class);
		Row formrow = RowConvertUtil.toJSONObjectLowerKey(formdata);
		formdata = formrow.toUnderlineKeysMap();
		if (!formdata.keySet().contains("type_name")) {
			throw new Exception("文档类型没有输入");
		}
		String typeName = formdata.get("type_name") == null ? formdata.get("TYPE_NAME").toString()
				: formdata.get("type_name").toString();
		Row row = documentService.create(typeName, formdata, file);
		return row.getString("id");
	}

	@Override
	public Map<String, Object> get(String id) throws Exception {
		Row row =documentService.getDocument(id);
		return row;
	}

	@Override
	public Boolean update(Map<String, Object> data) throws Exception {
		Boolean result = documentService.update(data);
		return result;
	}

	@Override
	public Boolean delete(String id) throws Exception {

		return null;
	}

	@Override
	public Page<Map<String, Object>> page(int pageSize, int pageIndex, JSONObject condition) throws Exception {
		return null;
	}

	@Override
	public List<Map<String, Object>> list(JSONObject condition) throws Exception {
		return null;
	}

}
